// MathOperation.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"

HMODULE g_hModule = NULL;

#define _OUTPUT_TRACEMSG_

void TraceMsg(const wchar_t*  szMsgFormat, ...)
{
#ifdef _OUTPUT_TRACEMSG_
	static wchar_t szMsgBuf[10240];
	va_list     ap;
	va_start(ap, szMsgFormat);
	vswprintf(szMsgBuf, 10240, szMsgFormat, ap);
	OutputDebugStringW(szMsgBuf);
	va_end(ap);
#endif //_OUTPUT_TRACEMSG_
}

BOOL RegistCom(const CLSID &clsid, const LPOLESTR pszDllPath, const LPOLESTR pszProgID)
{
	TraceMsg(L"RegistCom\n");
	wchar_t wszSubKey[MAX_PATH];
	LPOLESTR pszCLSID = NULL;
	StringFromCLSID(clsid, &pszCLSID);
	wsprintf(wszSubKey, L"CLSID\\%s",pszCLSID);
	CoTaskMemFree(pszCLSID);
	pszCLSID = NULL;
	HKEY hKeyCLSID = NULL;
	LONG lRet = ::RegCreateKeyEx(HKEY_CLASSES_ROOT, wszSubKey, 0, NULL, 0, KEY_WRITE, NULL, &hKeyCLSID, NULL);
	if (lRet == ERROR_SUCCESS)
	{
		::RegSetKeyValue(hKeyCLSID, L"InProcServer32", NULL, REG_SZ, (BYTE*)pszDllPath, wcslen(pszDllPath)*sizeof(wchar_t));
		::RegSetKeyValue(hKeyCLSID, L"ProgID", NULL, REG_SZ, (BYTE*)pszProgID, wcslen(pszProgID)*sizeof(wchar_t));
		::RegSetKeyValue(hKeyCLSID, L"VersionIndependentProgID", NULL, REG_SZ, (BYTE*)pszProgID, wcslen(pszProgID)*sizeof(wchar_t));
		::RegCloseKey(hKeyCLSID);
	}
	return TRUE;
}

BOOL UnRegistCom(const CLSID &clsid)
{
	TraceMsg(L"UnRegistCom\n");
	wchar_t wszSubKey[MAX_PATH];
	LPOLESTR pszCLSID = NULL;
	StringFromCLSID(clsid, &pszCLSID);
	wsprintf(wszSubKey, L"CLSID");
	HKEY hKeyCLSID = NULL;
	LONG lRet = ::RegOpenKeyEx(HKEY_CLASSES_ROOT, wszSubKey, 0, KEY_WRITE, &hKeyCLSID);
// 	LONG lRet = ::RegOpenKey(HKEY_CLASSES_ROOT, wszSubKey, &hKeyCLSID);
	if (lRet == ERROR_SUCCESS)
	{
		lRet = ::RegDeleteKeyEx(hKeyCLSID, pszCLSID, KEY_WOW64_32KEY, 0);
// 		lRet = ::RegDeleteKey(hKeyCLSID, pszCLSID);
		::RegCloseKey(hKeyCLSID);
	}
	CoTaskMemFree(pszCLSID);
	pszCLSID = NULL;
// 	::RegDeleteKeyEx(HKEY_CLASSES_ROOT, wszSubKey, KEY_WOW64_32KEY, 0);
	return FALSE;
}